<?php
namespace app\wxapp\controller;

use think\Db;


class Login 
{   
   

    //验证授权码登陆
    public function index(){
        $Wxapp_user   = db('wxapp_user');
        $Wxapp_config = db('wxapp_config');
        //获取授权码
        $auth_code  = input('auth_code');
        $session_id = input('session_id');
        $wx_user_info = $Wxapp_user->where('session_id',$session_id)->find();
        
        //验证授权码是否存在，存在返回赛事信息
        if($result = db('event')->where('auth_code',$auth_code)->find()){
            // if($wx_user_info['overtime'] < time()){
            //     $update['overtime'] = strtotime(date('Y-m-d',time()))+100800;
            // }
            $update['auth_code']    = $auth_code;
            //计算登录次数
            $update['login_times'] = (int)($wx_user_info['login_times'])+1;
            //计算登录时间
            $update['last_login_time'] = time();
            //将授权码插入wxapp的表中
            $Wxapp_user->where('id',$wx_user_info['id'])->update($update);
            //显示端登录日志
            // $consumption_info['use_time']      = time();
            // $consumption_info['use_date']      = date('Ymd',time());
            // $consumption_info['event_id']      = $result['id'];
            // $consumption_info['auth_code']     = $auth_code;
            // $consumption_info['wx_nick_name']  = $wx_user_info['nickName'] ? $wx_user_info['nickName'] : '';
            // $consumption_info['wx_avatar_url'] = $wx_user_info['avatarUrl'] ? $wx_user_info['avatarUrl']:'';
            // $consumption_info['wx_openid']     = $wx_user_info['openid'] ? $wx_user_info['openid']:''; 
            // $consumption_info['customer_name'] = $result['customer_name'];
            // $consumption_info['customer_mobile'] = $result['customer_mobile'];
          
            // Db::name('consumption')->insert($consumption_info);
            $data['state']      = 1;
            $data['info']       = '授权码通过';
            $data['event_id']   = $result['id']; //赛事id
            $result['begin_time'] = date('Y-m-d',strtotime($result['begin_time']));
            $result['end_time']   = date('Y-m-d',strtotime($result['end_time']));
            $data['event_info'] = $result;
            
            $data['wxapp_config']   = $Wxapp_config->where('id',1)->find();
            //$data['consumption_id'] = Db::name('consumption')->getLastInsID();

        }else{
            $data['state']     = 0;
            $data['info'] = '授权码不正确';
        }
        return json($data);
    }

    //微信小程序登陆
    public function wxLogin(){
        $appid   = "wxde90b3bc4e2a3fe4&secret";
        $secret  = "4463137786d1525af4a2b371215d1712";
        $code    = input('code');
        $rawdata_arr = json_decode(input('rawData'),true);
       
        $url         = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
        $result      = json_decode(file_get_contents($url),true);//获取用户openid与session_key
        
        $wxapp_user  = is_array($rawdata_arr) ? array_merge($rawdata_arr,$result) : $result;
      
        $wxapp_user['session_id'] = $update_info['session_id'] = getRandCode(20);
        //用户设备信息相关参数
        $wxapp_user['model']       = $update_info['model']   = input('model') ? input('model') : '';
        $wxapp_user['version']     = $update_info['version'] = input('version') ? input('version') : '';
        $wxapp_user['system']      = $update_info['system']  = input('system') ? input('system') : '';
        $wxapp_user['networkType'] = $update_info['networkType'] = input('networkType') ? input('networkType') : '';

        $update_result = $insert_result = false;
        //$wxapp_user['overtime'] = strtotime(date('Y-m-d',time()))+100800;//每天的100800秒让用户重新授权码登陆;
        $data['auth_code'] ='';
        if($result = db('wxapp_user')->where('openid',$wxapp_user['openid'])->find()){
            $update_info['last_visit_time'] = time();
            $update_info['visit_times']     = (int)$result['visit_times']+1;
            //如果wxapp_user 中存在授权码，且未超时，则绕过登陆
            // if($result['auth_code'] && $result['overtime']>time()){$data['auth_code'] = $result['auth_code'];}
            $update_info = is_array($rawdata_arr) ? array_merge($rawdata_arr,$update_info) : $update_info;
            $update_result = db('wxapp_user')->where('openid',$wxapp_user['openid'])->update($update_info);
        }else{
            unset($wxapp_user['expires_in']);
            $wxapp_user['last_visit_time'] = $wxapp_user['create_time'];
            $wxapp_user['visit_times'] = 1;
            $insert_result = db('wxapp_user')->insert($wxapp_user);
        }
        if($update_result || $insert_result){
            $data['session_id'] = $wxapp_user['session_id'];
            $data['state'] = 1;
            $data['info']  = '登陆成功！';
        }else{
            $data['state'] = 0;
            $data['state'] = '登陆失败！';
        }
        return json($data);
    }


}
